using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Lapas.Common
{
    public class DefaultForm
    {
        public static void DefaultForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (e.KeyCode == Keys.Escape)
            {
                BindDefaultForm((Form)sender);
            }
            if (e.KeyCode == Keys.F10)
            {
                e.Handled = true;
                ((Form)sender).Close();
            }
        }
        public static void BindDefaultForm(Form form)
        {
            foreach (Control control in form.Controls)
            {
                if (control != null)
                {
                    Control childControl = control.GetNextControl(control, true);
                    while (childControl != null)
                    {
                        if (childControl.TabIndex == 0) childControl.Select();
                        if (childControl.GetType() == typeof(TextBox)) ((TextBox)childControl).Text = string.Empty;
                        if (childControl.GetType() == typeof(MaskedTextBox)) ((MaskedTextBox)childControl).Text = string.Empty;
                        if (childControl.GetType() == typeof(ComboBox))
                        {
                            ((ComboBox)childControl).SelectedIndex = -1;
                        }
                        childControl = control.GetNextControl(childControl, true);
                    }
                    if (control.GetType() == typeof(TextBox)) ((TextBox)control).Text = string.Empty;
                    if (control.GetType() == typeof(ComboBox)) ((ComboBox)control).SelectedIndex = -1;
                    if (control.GetType() == typeof(MaskedTextBox)) ((MaskedTextBox)control).Text = string.Empty;
                    if (control.TabIndex == 0) control.Select();
                }

            }
        }

        public static void DefaultForm_Load(object sender, EventArgs e)
        {
            foreach (Control control in ((Form)sender).Controls)
            {
                if (control != null)
                {
                    Control childControl = control.GetNextControl(control, true);
                    while (childControl != null)
                    {
                        if (childControl.TabIndex == 0) childControl.Select();
                        if (childControl.GetType() == typeof(TextBox)) ((TextBox)childControl).Text = string.Empty;
                        if (childControl.GetType() == typeof(MaskedTextBox)) ((MaskedTextBox)childControl).Text = string.Empty;
                        if (childControl.GetType() == typeof(ComboBox))
                        {
                            ((ComboBox)childControl).SelectedIndex = -1;
                        }
                        childControl = control.GetNextControl(childControl, true);
                    }
                    if (control.GetType() == typeof(TextBox)) ((TextBox)control).Text = string.Empty;
                    if (control.GetType() == typeof(ComboBox)) ((ComboBox)control).SelectedIndex = -1;
                    if (control.GetType() == typeof(MaskedTextBox)) ((MaskedTextBox)control).Text = string.Empty;
                    if (control.TabIndex == 0) control.Select();
                }


            }
        }
    }
}
